library(ComplexHeatmap)
## Loading required package: grid
## ========================================
## ComplexHeatmap version 2.10.0
## Bioconductor page: http://bioconductor.org/packages/ComplexHeatmap/
## Github page: https://github.com/jokergoo/ComplexHeatmap
## Documentation: http://jokergoo.github.io/ComplexHeatmap-reference
## 
## If you use it in published research, please cite:
## Gu, Z. Complex heatmaps reveal patterns and correlations in multidimensional 
##   genomic data. Bioinformatics 2016.
## 
## The new InteractiveComplexHeatmap package can directly export static 
## complex heatmaps into an interactive Shiny app with zero effort. Have a try!
## 
## This message can be suppressed by:
##   suppressPackageStartupMessages(library(ComplexHeatmap))
## ========================================
library(patchwork)
## Warning: package 'patchwork' was built under R version 4.1.2
meta_df_dev <- readRDS('../separate_metadata_dev.rds')
meta_df_reg <- readRDS('../separate_metadata_reg.rds')
meta_df <- rbind(meta_df_dev, meta_df_reg)

mat_list <- readRDS('FigS20_mat_list.rds')
# this chunk may take a few minutes
p_list <- lapply(names(mat_list), function(x){
  print(x)
  mat <- mat_list[[x]]
  
  meta_df_sub <- meta_df[meta_df$orig.ident == x & (meta_df$CellType2 %in% c('AER','CT')),]
  celltype <- as.character(meta_df_sub$CellType2[match(colnames(mat), rownames(meta_df_sub))])
  
   ha <- HeatmapAnnotation(
        CellType2 = celltype,
        col = list(CellType2 = c("AER" = "#F0027F", "CT" = "#A6CEE3")),
        show_annotation_name = FALSE
    )

    p <- Heatmap(mat,
        name = 'Expression',
        column_title = x,
        show_column_names = FALSE, 
        show_row_names = FALSE,
        column_split = celltype,
        top_annotation = ha,

        # cluster_rows = FALSE,
        clustering_distance_rows = "euclidean",
        clustering_method_rows = "ward.D2",
        clustering_distance_columns = "euclidean",
        clustering_method_columns = "ward.D2",
        row_dend_reorder = TRUE,
        column_dend_reorder = TRUE,
        show_row_dend = FALSE,
        col = circlize::colorRamp2(c(-3, 0, 3), c("#2166ac", "white", "#b2182b")),
        use_raster = TRUE
    )
    
    grid.grabExpr(draw(p)) 
})
## [1] "Axolotl_ST50"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Axolotl_ST52"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Axolotl_ST54"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Chicken_E45"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Frog_NF50"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Frog_NF51"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Frog_NF52"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Frog_NF54"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Frog_reg"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Human_CS13"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Axolotl_reg"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Mouse_E105"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Mouse_E115"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Mouse_E125"
## Warning: The input is a data frame, convert it to a matrix.
## [1] "Mouse_E95"
## Warning: The input is a data frame, convert it to a matrix.
wrap_plots(p_list, guides = 'collect')